package com.zhangyongyi.carhome.adapter.recommend.technology;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.entity.greendao.NewesListEntity;
import com.zhangyongyi.carhome.tools.MyOnClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dllo on 16/1/18.
 */
public class TechnologyRecyclerViewAdapter extends RecyclerView.Adapter {
    private RequestQueue queue;
    private List<NewesListEntity> data;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private LruCache<String,Bitmap> lruCache;
    private Context context;

    //*----*
    private MyOnClickListener myOnClickListener;

    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    public TechnologyRecyclerViewAdapter(Context context) {
        this.context = context;
        queue = Volley.newRequestQueue(context);
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        int cacheSize = maxMemory / 4;
        lruCache =new LruCache<>(cacheSize);

        data =new ArrayList<>();

        imageCache =new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            lruCache.put(url,bitmap);
            }
        };
        imageLoader = new ImageLoader(queue,imageCache);
    }

    public void addData(List<NewesListEntity> datas) {
        data = datas;
        notifyDataSetChanged();
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_recommend_technology_one,parent,false);
            MainViewHolder mainViewHolder = new MainViewHolder(view);
            return mainViewHolder;
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_recommend_technology_two,parent,false);
            OtherViewHolder otherViewHolder = new OtherViewHolder(view);
            return otherViewHolder;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == 0){
            ((MainViewHolder)holder).title.setText(data.get(position).getTitle());
            ((MainViewHolder)holder).time.setText(data.get(position).getTime());
            ((MainViewHolder)holder).replycount.setText(String.valueOf(data.get(position).getReplycount()));
            String imgUrl = data.get(position).getSmallpic();
            getBitmapCache(imgUrl,((MainViewHolder) holder).imgIv);

        }else{
            ((OtherViewHolder)holder).title.setText(data.get(position).getTitle());
            ((OtherViewHolder)holder).time.setText(data.get(position).getTime());
            ((OtherViewHolder)holder).replycount.setText(String.valueOf(data.get(position).getReplycount()));
            String imgUrl = data.get(position).getIndexdetail();
            String split = "㊣";
            StringTokenizer token = new StringTokenizer(imgUrl, split);
            ArrayList<String> strings = new ArrayList<>();
            while (token.hasMoreTokens()) {
                strings.add(token.nextToken());
            }
            for (int i = 0; i < strings.size(); i++) {
                String url_a = strings.get(0);
                String url_b = strings.get(1);
                Log.i("aa",url_b);
                String url_c = strings.get(2);
                getBitmapCache(url_a,((OtherViewHolder) holder).imageView_a);
                getBitmapCache(url_b,((OtherViewHolder) holder).imageView_b);
                getBitmapCache(url_c,((OtherViewHolder) holder).imageView_c);
            }


        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myOnClickListener.MyOnClickListener(data.get(position).getId());
            }
        });

    }
    public void getBitmapCache(String url, ImageView imageView) {
        //* 构建图片监听器
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(
                imageView, R.mipmap.nissan,
                R.mipmap.nissan);
        //* 图片拉取
        imageLoader.get(url, listener);	//*
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getMediatype();
    }
    class MainViewHolder extends RecyclerView.ViewHolder{
        private ImageView imgIv;
        private TextView title, time, replycount;

        public MainViewHolder(View itemView) {
            super(itemView);
            imgIv = (ImageView) itemView.findViewById(R.id.iv_technology_one_item);
            title = (TextView) itemView.findViewById(R.id.tv_title_item_technology_one);
            time = (TextView) itemView.findViewById(R.id.tv_time_item_technology_one);
            replycount = (TextView) itemView.findViewById(R.id.tv_replyCount_item_technology_one);
        }
    }
    class OtherViewHolder extends RecyclerView.ViewHolder{
        private TextView title, time, replycount;

        ImageView imageView_a,imageView_b,imageView_c;
        public OtherViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_title_item_technology_two);
            time = (TextView) itemView.findViewById(R.id.tv_time_item_technology_two);
            replycount = (TextView) itemView.findViewById(R.id.tv_replyCount_item_technology_teo);

            imageView_a = (ImageView) itemView.findViewById(R.id.iv_a_technology_two_item);
            imageView_b = (ImageView) itemView.findViewById(R.id.iv_b_technology_two_item);
            imageView_c = (ImageView) itemView.findViewById(R.id.iv_c_technology_two_item);
        }
    }
}